<?php

namespace App\Admin\Controllers;

use App\Models\Shop;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Show;
use App\Admin\Extensions\Shop\Failed;
use App\Admin\Extensions\Shop\Success;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class ShopNewController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('店铺众筹申请')
            ->description('店铺众筹申请')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Shop);

        $grid->model()->where('status',0)->orderBy('id','desc');
        $grid->id('Id');
        $grid->shop_type('店铺类型')->display(function ($value) {
            switch ($value) {
                case '1':
                    return "<span class='label label-warning'>线上</span>";
                    break;
                case '2':
                    return "<span class='label label-success'>线下</span>";
                    break;
                default:
                    # code...
                    break;
            }
        });
        $grid->address('地址')->expand(function ($model) {

            $comments = $model->where('id',$this->id)->get()->map(function ($comment) {             
                $comment->crowd_funding_start = date('Y-m-d',$comment->crowd_funding_start);
                return $comment->only(['id', 'promoter','shop_name','phone','wechat','area','crowd_funding_start']);;
            });

            return new Table(['ID', '发起人','店铺名称','手机号','微信','面积(m²)','发起时间'], $comments->toArray());
        });
        $grid->shop_price('总金额');
        $grid->shop_info('店铺图片')->gallery(['width' => 50]);

        $grid->status('状态')->display(function($value){
            switch ($value) {
                case '0':
                    return "<span class='label label-warning'>待审核</span>";
                    break;
                case '1':
                    return "<span class='label label-success'>众筹中</span>";
                    break;
                case '2':
                    return "<span class='label label-primary'>未通过</span>";
                    break;
                case '3':
                    return "<span class='label label-danger'>众筹成功</span>";
                    break;
                case '4':
                    return "<span class='label label-danger'>众筹失败</span>";
                    break;
                
                default:
                    # code...
                    break;
            }
        });
        $grid->created_at('创建时间');

        $grid->disableCreateButton();

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
            // 添加操作
            $actions->append(new Success($actions->getKey()));
            $actions->append(new Failed($actions->getKey()));
        });

        $grid->filter(function ($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->scope('线上店铺')->where('shop_type', 1);
            $filter->scope('线下店铺')->where('shop_type', 2);
            $filter->like('shop_name','店铺名称');
            $filter->like('phone', '手机号');
            // 在这里添加字段过滤器
            $filter->between('created_at', '申请时间')->datetime();

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Shop::findOrFail($id));

        $show->id('Id');
        $show->longitude('Longitude');
        $show->latitude('Latitude');
        $show->address('Address');
        $show->shop_info('Shop info');
        $show->shop_price('Shop price');
        $show->crowd_fund('Crowd fund');
        $show->manager('Manager');
        $show->waiter('Waiter');
        $show->model('Model');
        $show->supplier('Supplier');
        $show->status('Status');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Shop);

        $form->decimal('longitude', 'Longitude');
        $form->decimal('latitude', 'Latitude');
        $form->text('address', 'Address');
        $form->text('shop_info', 'Shop info');
        $form->decimal('shop_price', 'Shop price');
        $form->decimal('crowd_fund', 'Crowd fund');
        $form->text('manager', 'Manager');
        $form->text('waiter', 'Waiter');
        $form->text('model', 'Model');
        $form->text('supplier', 'Supplier');
        $form->number('status', 'Status');

        return $form;
    }

    //审核通过
    public function success(Request $request)
    {
        $id = $request->id;
        $shop = Shop::findOrFail($id);


        DB::beginTransaction();
        try {
            $shop->status = 1;
            $shop->save();

            DB::commit();

            return $this->ajax_success('操作成功');

        } catch (\Exception $exception) {

            DB::rollBack();

            return $this->ajax_error('操作失败');

        }

    }
    //审核不通过
    public function failed(Request $request)
    {
        $id = $request->id;
        $shop = Shop::findOrFail($id);
        $shop->status = 3;
        $shop->comment = $request->comment;
        $res = $shop->save();
        if ($res) {
            return $this->ajax_success('操作成功');
        } else {
            return $this->ajax_error('操作失败');
        }

    }

}
